from selenium import webdriver
from selenium.webdriver.common.by import  By
import time
import re
browser = webdriver.Chrome()
browser.maximize_window() # 窗口最大化
url = 'https://item.jd.com/100011690068.html' # 京东商城手机商品页面
browser.get(url)
# 手动登录京东商城
time.sleep(30)
browser.find_element(By.CSS_SELECTOR, 'li[data-anchor="#comment"]').click()
time.sleep(5)
browser.find_element(By.CSS_SELECTOR, 'input#comm-curr-sku').click()
p_comment='<p class="comment-item">(.*?)</p>'
for j in range(5):
    time.sleep(5)
    code=browser.page_source
    comment=re.findall(p_comment,code,re.S)
    comment=[i.strip().replace('<br>','') for i in comment]
    with open(file='京东商城用户评价-多页.txt',mode='a',encoding='utf-8') as f:
        s=f.write('\n'.join(comment))
        f.write('\n')
    browser.find_element(By.CSS_SELECTOR, 'div.com-table-footer a.ui-pager-next').click()
browser.quit()
